Skip to content

fix(docreader): escape table cell pipes in DOCX tables - #6103

Open
zuizhuai wants to merge 1 commit into
Tencent:mainfrom
zuizhuai:fix/docx-table-pipes
Open

zuizhuai wants to merge 1 commit into
Tencent:mainfrom
zuizhuai:fix/docx-table-pipes

Conversation

@zuizhuai

Copy link
Copy Markdown
Contributor

Description

A DOCX table cell that contains | is read as two columns. Docx2Parser prefers MarkItDown, whose DocxConverter builds the table through mammoth HTML and then writes it with markdownify; markdownify copies the character as it is, so the row gains a cell that the delimiter row does not have and every later cell shifts one column right.

A Word table with the header Plan, Price | Currency, Notes currently comes out as:

| Plan | Price | Currency | Notes |
| --- | --- | --- | --- |
| Basic | 10 | USD | x |

Expected:

| Plan | Price \| Currency | Notes |
| --- | --- | --- |
| Basic | 10 \| USD | x |

The same problem was fixed for CSV (#5392), EPUB (#5523) and PPTX (#6019). Those formats register a converter that escapes the pipe before the table is written. DOCX still goes through the stock DocxConverter, so it was left out. The fallback DocxParser escapes pipes in _gfm_cell_text already, which is why only the default MarkItDown path is affected — a document that fails over to the fallback renders the cell correctly today.

Change

DocxConverter.convert hands the mammoth HTML to the instance attribute _html_converter, so EscapedDocxConverter replaces that attribute with an HtmlConverter that escapes the cells first. The conversion logic itself is still MarkItDown's: nothing is copied from the library.

The cell-level escaping is MHTMLParser._escape_table_cell_pipes, which HTML, EPUB and MHTML documents already use, so DOCX now shares that implementation instead of adding a fourth copy.

A document whose HTML has no <table takes the untouched path, so there is no extra BeautifulSoup parse for the common case.

Tests

docreader/tests/test_docx_table_cells.py mirrors test_pptx_table_cells.py: a cell holding Price | Currency must stay one column, and a plain table must render exactly as before.

$ python -m unittest discover -s docreader/tests -p "test_docx_table_cells.py" -v
test_pipe_in_cell_stays_in_one_column ... ok
test_plain_table_is_unchanged ... ok

Without the converter registration the first test fails with the split row shown above and the second still passes, so the test pins the defect rather than the implementation.

Full suite on the locked environment (uv sync --locked, Python 3.10.18, markitdown 0.1.3):

Ran 653 tests in 44.791s
FAILED (errors=1, skipped=15)

The single error is test_ssrf_proxy.test_webkit_redirects_and_subresources_use_proxy, which needs the Playwright WebKit build and fails the same way on main without it.

MarkItDown's DocxConverter builds a table through mammoth HTML and then
writes it with markdownify, which leaves a "|" inside a cell's text as it
is. The reader takes that pipe for a column delimiter, so a cell holding
"10 | USD" splits into two columns and every later cell shifts right.

CSV (Tencent#5392), EPUB (Tencent#5523) and PPTX (Tencent#6019) already wrap their converter
for this reason; DOCX still went through the stock converter. The
fallback DocxParser escapes pipes itself, so only the default MarkItDown
path was affected.

DocxConverter hands the mammoth HTML to the _html_converter instance
attribute, so replacing that attribute with one that escapes the cells
first is enough: the conversion itself stays MarkItDown's, and a document
without a table takes the untouched path.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant